﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    class LogicPoints:Collection
    {
        public Point m_Center = new Point();
        public List<Point> m_Points = new List<Point>();

        public LogicPoints(House house):base(house)
        {
        }
        public override void AddData(Entity value)
        {
            base.AddData(value);
            if (value is Autodesk.AutoCAD.DatabaseServices.Line)
            {
                Autodesk.AutoCAD.DatabaseServices.Line entity = value as Autodesk.AutoCAD.DatabaseServices.Line;
                Point pSta = new Point(entity.StartPoint);
                Point pEd = new Point(entity.EndPoint);
                if (m_Points.Find(new Predicate<Point>(o => o == pSta)) == null)
                {
                    pSta.index = m_Points.Count;
                    m_Points.Add(pSta);
                }
                else
                {
                    m_Points[m_Points.IndexOf(pSta)].refCount++;
                }
                //查找点
                if (m_Points.Find(new Predicate<Point>(o => o == pEd)) == null)
                {
                    pEd.index = m_Points.Count;
                    m_Points.Add(pEd);
                }
                else
                {
                    m_Points[m_Points.IndexOf(pEd)].refCount++;
                }
            }
            else if (value is Autodesk.AutoCAD.DatabaseServices.Polyline)
            {
                Autodesk.AutoCAD.DatabaseServices.Polyline entity = value as Autodesk.AutoCAD.DatabaseServices.Polyline;
                for (int i = 0; i < entity.NumberOfVertices; i++)
                {
                    if (SegmentType.Line == entity.GetSegmentType(i))
                    {
                        Point pSta = new Point(entity.GetLineSegmentAt(i).StartPoint);
                        Point pEd = new Point(entity.GetLineSegmentAt(i).EndPoint);
                        if (m_Points.Find(new Predicate<Point>(o => o == pSta)) == null)
                        {
                            pSta.index = m_Points.Count;
                            m_Points.Add(pSta);
                        }
                        else
                        {
                            m_Points[m_Points.IndexOf(pSta)].refCount++;
                        }
                        //查找点
                        if (m_Points.Find(new Predicate<Point>(o => o == pEd)) == null)
                        {
                            pEd.index = m_Points.Count;
                            m_Points.Add(pEd);
                        }
                        else
                        {
                            m_Points[m_Points.IndexOf(pEd)].refCount++;
                        }
                    }
                }
            }
        }
        public override void AjustLineAndPoint()
        {
            //矫正数据记录原点
            Point center = m_house.Item<Points>().m_Center;
            m_Points.ForEach(o => o.Offset(center));
            //墙线和构造线合并
            m_Points.AddRange(m_house.Item<Points>().m_Points.Where(o=>!m_Points.Contains(o)).Clone<Point>());
            //修改索引
            m_Points.ForEach(o => o.index=m_Points.IndexOf(o));
        }
        public override IEnumerable<XElement> ToXml()
        {
            foreach (Point pt in m_Points)
                yield return new XElement("_" + m_Points.IndexOf(pt), pt.ToMirror().ToXml());
        }
    }
}
